//
//  BookViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-9.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "BookViewController.h"
#import "CommentBookViewController.h"
#import "AccountViewController.h"
#import "AppDelegate.h"
@interface BookViewController ()

@end

@implementation BookViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotBookInfo:) name:gotBookInfoNotify object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    titleLabel.text = self.itemname;
    contentScrollView.contentSize = CGSizeMake(960, 400);
    if (bookInfoViewController == nil) {
        bookInfoViewController = [[BookInfoViewController alloc] initWithNibName:@"BookInfoViewController" bundle:nil];
        bookInfoViewController.itemid = self.itemid;
        bookInfoViewController.view.frame = contentScrollView.bounds;
        [contentScrollView addSubview:bookInfoViewController.view];
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // Do any additional setup after loading the view from its nib.
    
    if (bookContentViewController == nil) {
        bookContentViewController = [[BookContentViewController alloc] initWithNibName:@"BookContentViewController" bundle:nil];
        bookContentViewController.itemid = self.itemid;
        CGRect rect = contentScrollView.bounds;
        rect.origin.x = 320;
        bookContentViewController.view.frame = rect;
        [contentScrollView addSubview:bookContentViewController.view];
    }
    if (bookCommentViewController == nil) {
        bookCommentViewController = [[BookCommentViewController alloc] initWithNibName:@"BookCommentViewController" bundle:nil];
        bookCommentViewController.itemid = self.itemid;
        CGRect rect = contentScrollView.bounds;
        rect.origin.x = 640;
        bookCommentViewController.view.frame = rect;
        [contentScrollView addSubview:bookCommentViewController.view];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    currentPage = contentScrollView.contentOffset.x/320;
    contentPageControl.currentPage = currentPage;
    
    if (currentPage == 2) {
        homeButton.hidden = YES;
        commentButton.hidden = NO;
    }
    else {
        homeButton.hidden = NO;
        commentButton.hidden = YES;
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate) {
        [self scrollViewDidEndDecelerating:contentScrollView];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self scrollViewDidEndDecelerating:contentScrollView];
}

- (IBAction)showCommentView:(id)sender
{
    if ([[AuthorManager defaultManager] hasLoggedIn]) {
        CommentBookViewController *commentBookViewController = [[[CommentBookViewController alloc] initWithNibName:@"CommentBookViewController" bundle:nil] autorelease];
        commentBookViewController.itemid = self.itemid;
        commentBookViewController.itemname = self.itemname;
        UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
        [navController pushViewController:commentBookViewController animated:YES];
    } else {
//        AppDelegate * appD = (AppDelegate *)[[UIApplication sharedApplication]delegate];
        AccountViewController *accountViewController = [[[AccountViewController alloc] initWithNibName:@"AccountViewController" bundle:nil] autorelease];
        accountViewController.delegate = self;
        accountViewController.needLogin = YES;
        accountViewController.wantsFullScreenLayout = YES;
        [self.view.window.rootViewController presentViewController:accountViewController animated:YES completion:nil];
    }
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    viewController.wantsFullScreenLayout = YES;
}

- (IBAction)navBack
{
//    if (currentPage != 0) {
//        [contentScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
//        return;
//    }
    UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
    [navController popViewControllerAnimated:YES];
}
- (IBAction)navBackToHome
{
    UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
    [navController popToRootViewControllerAnimated:YES];
}

- (void)gotBookInfo:(NSNotification *)notification
{
    if ([notification.object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *result = [notification.object valueForKey:@"result"];
        if ([result isKindOfClass:[NSDictionary class]]) {
            titleLabel.text = [result objectForKey:@"item_name"];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [contentScrollView release];
    [_itemid release];
    [_itemname release];
    [bookInfoViewController release];
    [bookContentViewController release];
    [contentPageControl release];
    [titleLabel release];
    [homeButton release];
    [commentButton release];
    [super dealloc];
}
- (void)viewDidUnload {
    [contentScrollView release];
    contentScrollView = nil;
    [contentPageControl release];
    contentPageControl = nil;
    [titleLabel release];
    titleLabel = nil;
    [homeButton release];
    homeButton = nil;
    [commentButton release];
    commentButton = nil;
    [super viewDidUnload];
}
@end
